package com.example.springmysqldemo.dao;

import com.example.springmysqldemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoimpl implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public List<User> findAllUsers() {
        String sql = "select * from t_user";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return users;
    }

    @Override
    public User getUserInfo(String username, String password) {
        String sql = "select * from t_user where username = ? and password = ?";
        Object [] args =  {username,password};
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), args);
        return user;
    }
}
